<?php
/**
 * Created by PhpStorm.
 * User: WUHENG
 * Date: 2022/9/26
 * Time: 10:05
 */

namespace app\admin\controller;


use app\conmon\validate\admAddValidate;
use app\conmon\validate\admValidate;
use app\index\model\Article;
use app\index\model\Comments;
use app\index\model\Type;
use app\index\model\User;
use think\Controller;

class Admin extends Controller
{
    protected function initialize()
    {
        header('Access-Control-Allow-Origin:http://127.0.0.1:5500');
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function verify(){
        if(!session('name')){
            $this->error('你还没有登录','http://cms.com:8081/admin/admin/showLogin');
        }
    }
    public function showFront(){
        $this->success('','http://127.0.0.1:5500/index.html');
    }
    public function showHome()
    {
        $this->verify();
        return $this->fetch('index');
    }

    public function showLogin()
    {
        return $this->fetch('login');
    }

    public function showSignUp()
    {
        return $this->fetch('sign-up');
    }

    public function showForm()
    {
        $this->verify();
        return $this->fetch('form');
    }

//    后台管理员登录
    public function adminLogin()
    {
        $validate = new admValidate();
        $data = input('post.');
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data['admPass'] = md5(md5($data['admPass']));
        $res = \app\admin\model\Admin::where(['admPhone' => $data['admPhone'], 'admPass' => $data['admPass']])->find();
        if ($res['admState'] == 0) {
            echo "<script>alert('账号已被禁用，请联系管理员！');window.history.back(-1);</script>";
            die;
        }
        if ($res) {
            session('name', $res);
            $user = new  \app\admin\model\Admin();
            $user->save(['tt' => 1], ['admID' => $res['admID']]);
            $this->success('登录成功', 'http://cms.com:8081/admin/admin/showHome');
        } else {
            echo "<script>alert('登录失败！');window.history.back(-1);</script>";
            die;
        }
    }


    public function admOut()
    {
        session(NULL);
        return $this->error('退出成功！', 'http://cms.com:8081/admin/admin/showLogin');

    }

    public function admAdd()
    {
        $this->verify();
        $validate = new admAddValidate();
        $data = input('post.');
        $am = input('get.user');
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $id = \app\admin\model\Admin::where('admID',$am)->find();
        $adm = \app\admin\model\Admin::where('admAuthor','超级管理员')->find();
        if($id['admAuthor'] != '超级管理员'){
            echo "<script>alert('你没有权限！');window.history.back(0);</script>";
            die;
        }
        $data['admPass'] = md5(md5($data['admPass']));
        $res = \app\admin\model\Admin::where('admPhone', $data['admPhone'])->find();
        if ($res) {
            echo "<script>alert('号码已经注册！');window.history.back(0);</script>";
            die;
        }
        $user = new \app\admin\model\Admin();
        $user->save($data);
        if ($user) {
            echo "<script>alert('增加成功！');window.history.back(0);</script>";
            die;
        } else {
            echo "<script>alert('增加失败！');window.history.back(0);</script>";
            die;
        }
    }

    public function admPhotoUp()
    {
        $this->verify();
        $id = input('get.id');
        $file = request()->file('image');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move('./static/uploads');
        $user = new \app\admin\model\Admin();
        $user->save(['admAvatar' => $info->getSaveName()], ['admID' => $id]);
        session('name.admAvatar', $info->getSaveName());
        return json_encode($info->getSaveName());
    }

    public function showCalendar()
    {
        $this->verify();
        $data = \app\admin\model\Admin::order('admID','DESC')->select();
        return view('calendar', ['data' => $data]);
    }

    public function admState()
    {
        $this->verify();
        $data = input('post.');
        $user = new \app\admin\model\Admin();
        $user->save(['admState' => $data['state']], ['admID' => $data['id']]);
        if ($user) {
            echo "<script>;alert('修改成功！');window.history.back(-1);</script>";
            die;
        } else {
            echo "<script>alert('修改失败！');window.history.back(-1);</script>";
            die;
        }
    }

    public function admDel()
    {
        $this->verify();
        $id = input('get.id');
        $user = \app\admin\model\Admin::where('admID',$id)->delete();
        if ($user) {
            echo "<script>alert('删除成功！');window.history.back(-1);</script>";die;
        }
//        dump($id);
    }
    public function admUpdatePass(){
        $this->verify();
        $data = input('post.');
        $data['old']=md5(md5($data['old']));
        $data['news']=md5(md5($data['news']));
        $res = \app\admin\model\Admin::where('admID',$data['id'])->find();
        if ($res['admPass'] != $data['old']){
            return 0;
        }else{
            $user = new \app\admin\model\Admin();
            $user->save([ 'admPass'  => $data['news']],['admID' => $data['id']]);
            if ($user) {
               return 1;
            }
        }
    }
    public function showTableList(){
        $this->verify();
        $data = Type::where('tID',0)->select();
        $vue = Type::where('tID','neq',0)->select();
        for ($i=0;$i<count($data);$i++){
            for ($j=0;$j<count($vue);$j++){
                $res[$j] = Type::where('tID',$data[$i]['typID'])->select();
                $data[$i]['res'] = $res[$j];
            }
        }
        return view('table-list',['data'=>$data]);
    }
    public function typAdd(){
        $this->verify();
        $data = input('post.');
        $user = new Type();
        $user->save([
            'typName'  =>  $data['typName'],
            'tID' =>  $data['typID']
        ]);
        if ($user){
            return 1;
        }else{
            return 0;
        }
    }
    public function firstTypAdd(){
        $this->verify();
        $data = input('post.');
        $user = new Type();
        $user->save([
            'typName'  =>  $data['typName'],
            'tID' =>  0
        ]);
        if ($user){
            return 1;
        }else{
            return 0;
        }
    }
    public function TypDel(){
        $this->verify();
        $id = input('post.typID');
        $data = Type::where('typID',$id)->find();
        if($data['tID'] != 0){
            Type::where('typID',$id)->delete();
            Article::where('typID',$id)->delete();
            return 0;
        }else{
            $vue = Type::where('typID',$id)->delete();
            if($vue){
                $my = Type::where('tID',$id)->select();
                for($i=0;$i<count($my);$i++){
                    Article::where('typID',$my[$i]['typID'])->delete();
                }
                Type::where('tID',$id)->delete();
                return 1;
            }else{
                return 2;
            }

        }


    }
    public function showTables(){
        $this->verify();
        $data = User::paginate(10);
        $page = $data->render();
        return view('tables',['data'=>$data,'page'=>$page]);
    }
    public function searchUser(){
        $this->verify();
        $input = input('post.input');
        $keywords = $input;
        if ($keywords) {
            $map = ['userName', 'like', '%' . $keywords . '%'];
            $map1 = ['userPhone', 'like', '%' . $keywords . '%'];
            $map2 = ['userState', 'like', '%' . $keywords . '%'];
            $vue = User::whereOr([$map, $map1,$map2])->select();
            return json_encode($vue);
        }
    }
    public function showTableListImg(){
        $this->verify();
        $data = Article::where('artState','neq',0)->paginate(15);
        foreach ($data as $v){
            $v->articleType;
        }
        $page = $data->render();
        $type = Type::where('tID',0)->select();
        $vue = Type::where('tID','neq',1)->select();
        for ($i=0;$i<count($type);$i++){
            for ($j=0;$j<count($vue);$j++){
                $res[$j] = Type::where('tID',$type[$i]['typID'])->select();
                $type[$i]['res'] = $res[$j];
            }
        }
        return view('table-list-img',['data'=>$data,'page'=>$page,'res'=>$type]);
    }
    public function searchType(){
        $this->verify();
        $value = input('post.value');
        $input = input('post.input');
        if($value == 0) {
            $keywords = $input;
            if ($keywords) {
                $map = ['artTitle', 'like', '%' . $keywords . '%'];
                $map1 = ['artContent', 'like', '%' . $keywords . '%'];
                $map2 = ['artAuthor', 'like', '%' . $keywords . '%'];
                $vue = Article::whereOr([$map, $map1, $map2])->where('artState', 1)->order('typID')->select();
                foreach ($vue as $v) {
                    $v->articleType;
                }
                return json_encode($vue);
            }
            if ($input == 0) {
                $data = Type::where('typName', $value)->find();
                $vue = Article::where('typID', $data['typID'])->select();
                foreach ($vue as $v) {
                    $v->articleType;
                }
                return json_encode($vue);
            }
        }

    }
    public function showFour(){
        $this->verify();
        $type = Type::where('tID',0)->select();
        $vue = Type::where('tID','neq',0)->select();
        for ($i=0;$i<count($type);$i++){
            for ($j=0;$j<count($vue);$j++){
                $res[$j] = Type::where('tID',$type[$i]['typID'])->select();
                $type[$i]['res'] = $res[$j];
            }
        }
        return $this->fetch('four',['type'=>$type]);
    }
    public function uploadArt(){
        $this->verify();
        $num = rand(2,9999);
        $data = input('post.');
        if($data['content'] == ''){
            echo "<script>alert('内容不能为空！');window.history.back(-1);</script>";die;
        }
        $time = date('y-m-d',strtotime('- ' . $num . ' day'));
        $user = new Article();
        $user->save([
            'typID' => $data['type'],
            'artTitle' => $data['title'],
            'artAuthor' => $data['author'],
            'artContent' => $data['content'],
            'dateline' => $time
        ]);
        if($user){
            echo "<script>alert('增加成功！');window.history.back(-1);</script>";die;
        }
    }
    public function showChart(){
        $this->verify();
        $data = Comments::paginate(15);
        foreach ($data as $u){
            $u->user;
        }
        $page = $data->render();
        return view('chart',['data'=>$data,'page'=>$page]);
    }
    public function commentDel(){
        $this->verify();
        $id = input('post.comtID');
        $data = Comments::where('comtID',$id)->find();
        if($data['hID'] != 0){
            Comments::where('comtID',$id)->delete();
            $art = Article::where('artID',$data['artID'])->find();
            $num = $art['comtNum']-1;
            $vue = new Article();
            $vue->save(['comtNum'=>$num],['artID'=>$data['artID']]);
            if($vue){
                return 1;
            }else{
                return 0;
            }
        }else{
            $del = Comments::where('comtID',$id)->delete();
            if($del){
                $res = Comments::where('hID',$id)->select();
                $n = count($res)+1;
                $art = Article::where('artID',$data['artID'])->find();
                $num = $art['comtNum']-$n;
                $vue = new Article();
                $vue->save(['comtNum'=>$num],['artID'=>$data['artID']]);
                Comments::where('hID',$id)->delete();
                return 1;
            }else{
                return 0;
            }
        }
    }
    public function contrUserState(){
        $this->verify();
        $id = input('post.');
        $user = new User();
        if($id['state'] == 1){
            $user->save(['userState'=>0],['uID'=>$id['uID']]);
        }else{
            $user->save(['userState'=>1],['uID'=>$id['uID']]);
        }
        if($user){
            return 1;
        }else{
            return 0;
        }
    }
    public function userDel(){
        $this->verify();
        $id = input('post.uID');
        $user = User::where('uID',$id)->delete();
        if($user){
            $comt = Comments::where('uID',$id)->select();
            if(count($comt) > 0){
                $comtid = Comments::field('comtID')->where('uID',$id)->select();
                Comments::where('hID','in',$comtid)->delete();
                Comments::where('uID',$id)->delete();
                return 1;
            }
            return 1;
        }else{
            return 0;
        }

    }
    public function artDown(){
        $this->verify();
        $data = input('post.');
        $time = date('y-m-d');
        $user = new Article();
        $user->save([
            'artState' => 0,
            'dateline'=>$time,
            'rfDown' => $data['text']
        ],['artID'=>$data]
        );
        if($user){
            return 1;
        }
        else{
            return 0;
        }
    }
    public function artDel(){
        $this->verify();
        $data = input('post.id');
        $art = Article::where('artID',$data)->delete();
        if($art){
            return 1;
        }
        else{
            return 0;
        }
    }
    public function showArtDown(){
        $this->verify();
        $data = Article::where('artState',0)->paginate(10);
        foreach ($data as $v){
            $v->articleType;
        }
        $page = $data->render();
        $type = Type::where('tID',0)->select();
        $vue = Type::where('tID','neq',1)->select();
        for ($i=0;$i<count($type);$i++){
            for ($j=0;$j<count($vue);$j++){
                $res[$j] = Type::where('tID',$type[$i]['typID'])->select();
                $type[$i]['res'] = $res[$j];
            }
        }
        return view('artdown',['data'=>$data,'page'=>$page,'res'=>$type]);
    }
    public function artUp(){$this->verify();
        $this->verify();
        $data = input('post.id');
        $time = date('y-m-d');
        $user = new Article();
        $user->save([
            'artState' => 1,
            'dateline'=>$time,
            'rfDown' => NULL
        ],['artID'=>$data]
        );
        if($user){
            return 1;
        }
        else{
            return 0;
        }
    }
    public function showTest(){
        return $this->fetch('test');
    }














//    public function uid(){
//        $data = User::select();
//        for($i=0;$i<count($data);$i++){
//            $user = new User();
//            $user->save(['useAavatar'=>'20221010\791f31aff95389a70c4760479d308e35.jpg'],['uID'=>$data[$i]['uID']]);
//
//        }
//    }











//    public function update(){
//        $data = Article::field('artAuthor')->group('artAuthor')->select();
//
//        for($i=0;$i<count($data);$i++){
//            $num = rand(2222,9999);
//            $userPhone = '1508888'.$num;
//            $pass = '123456';
//            $time = date('y-m-d',strtotime('- ' . $num . ' day'));
//            $user = new User();
//            $user->save([
//                'userName' => $data[$i]['artAuthor'],
//                'userPhone' => $userPhone,
//                'userPass' =>md5(md5($pass)),
//                'dateline' =>$time
//            ]);
//        }
//    }



























}